﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CashierSystem.CashierInManager;
using Excel = Microsoft.Office.Interop.Excel;

namespace CashierSystem.SearchAccountManager
{
    public partial class SearchAccountForm : Form
    {
        public SearchAccountForm()
        {
            InitializeComponent();
        }

        string[] departId;
        string[] departName;

        private void SearchAccountForm_Load(object sender, EventArgs e)
        {
            CashierInManager.CashierInManager.getDepartInfo(out departName, out departId);
            cbDepartment.Items.Clear();
            for (int i = 0; i < departId.Length; i++)
            {

                cbDepartment.Items.Add(departName[i]);
            }
            cbDepartment.SelectedIndex = 0;
            cbDepartment.Items[0] = "所有检测";
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            tbStartDate.Text = dtpStartDate.Value.Date.ToString();
        }

        private void dtpEndDate_ValueChanged(object sender, EventArgs e)
        {
            tbEndDate.Text = dtpEndDate.Value.Date.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = cbDepartment.SelectedIndex;
            string departId1 = departId[i].ToString();
            if (cbDepartment.SelectedIndex == 0)
            {
                departId1 = "";
            }
            string startDate = "";
            if (tbStartDate.Text != "")
            {
                startDate = tbStartDate.Text;

            }
            else
            {
                startDate = "";
            }
            string endDate = "";
            if (tbEndDate.Text != "")
            {
                endDate = tbEndDate.Text;
            }
            else
            {
                endDate = "";
            }
            SearchAccountManager.loadAccount1(listView1, departId1, startDate, endDate);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象，可能您的机器未安装Excel");
                return;
            }
            Excel.Workbooks workbooks = xlApp.Workbooks;
            Excel.Workbook workbook = workbooks.Add(true);
            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
            xlApp.Visible = true;
            //填充列
            for (int i = 0; i < listView1.Columns.Count; i++)
            {
                worksheet.Cells[1, i + 1] = listView1.Columns[i].Text.ToString();
                ((Excel.Range)worksheet.Cells[1, i + 1]).Font.Bold = true;
            }

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                for (int j = 0; j < listView1.Columns.Count; j++)
                {
                    if (j == 0)
                    {
                        worksheet.Cells[2 + i, j + 1] = listView1.Items[i].Text.ToString();
                        ((Excel.Range)worksheet.Cells[2 + i, j + 1]).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
                    }
                    else
                    {
                        worksheet.Cells[2 + i, j + 1] = listView1.Items[i].SubItems[j].Text.ToString();
                        ((Excel.Range)worksheet.Cells[2 + i, j + 1]).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
                    }
                }
            }
        }
    }
}